package dept_api

import (
	"benkendSystem/database"
	"benkendSystem/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

// UpdateDepart 更新部门信息
func UpdateDepart(c *gin.Context) {
	in := new(UpdateDepartRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	// 判断部门id是否为空
	if in.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	err = database.DB.Model(new(models.Department)).Where("id = ?", in.ID).Updates(map[string]any{
		"name":      in.Name,
		"parent_id": in.ParentID,
		"order":     in.Order,
	}).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "操作失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "操作成功",
	})
}
